package com.onpositive.wiki2.whole.model;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.onpositive.compactdata.CompactLongVector;
import com.onpositive.wiki2.miner.AbstractWikiPediaVisitor;
import com.onpositive.wiki2.miner.Page;

public class ContentTable extends AbstractWikiPediaVisitor {

	protected DataOutputStream s;

	protected CompactLongVector positions = new CompactLongVector();
	protected int up = 0;
	File ps;

	public ContentTable(String path) throws IOException {
		ps = new File(path);
		ps.mkdirs();
		File file = new File(ps, "content.dat");
		file.getParentFile().mkdirs();
		if(file.exists())
			file.delete();
		
		file.createNewFile();
		
		s = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
				file)));
	}

	public void close() throws IOException {
		s.close();
		positions.store(new File(ps, "content.list"));
	}

	long k = 0;

	@Override
	protected void onPage(Page page) {			
		positions.add(k);
		up++;
		try {
			byte[] bytes = page.textContent.getBytes("UTF-8");
			s.writeInt(bytes.length);
			s.write(bytes);
			k += (bytes.length + 4);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
